package gui;

import sensor.TempSensor;
import sensorlogic.SensorLogic;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by IntelliJ IDEA.
 * User: kate
 * Date: 22.05.2009
 * Time: 13:33:55
 * To change this template use File | Settings | File Templates.
 */
public class TempretureSensorForm extends JFrame {

    private static SensorLogic sensorLogicThread;

    public JPanel sensorPanel;
    public JPanel stratDevicePanel;
    public JPanel confSensorPanel;
    public JPanel logPanel;
    public JTextField portTextField;
    public JTextField addressTextField;
    public JButton startButton;
    public JRadioButton autoRadioButton;
    public JTextField timeTextField;
    public JRadioButton manualRadioButton;
    public JTextField tempTextField;
    public JButton okButton;
    public JTextPane logTextPane;
    public JLabel portLabel;
    public JLabel addressLabel;
    public JButton stopButton;
    public JLabel dataLabel;
    public JLabel timeLabel;
    public JLabel tempLabel;
    public JLabel secLabel;
    public JLabel celciumLabel;
    public JScrollPane logScrollPane;
    public JTextArea logTextArea;
    public JButton stopSendButton;
    public JRadioButton upButton;

    public boolean AutoRB;
    public TempSensor tempSensor;
    int i = 0;
    String temp;


    public static void run() {
        JFrame frame = new JFrame("TempretureSensorForm");
        TempretureSensorForm form = new TempretureSensorForm();
        frame.setContentPane(form.sensorPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

        sensorLogicThread = new SensorLogic(form);
        sensorLogicThread.start();
    }

    public TempretureSensorForm() {

        AutoRB = true;

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });

        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("ActionEvent: " + e);
                try {
                    stopButton.setEnabled(true);
                    autoRadioButton.setEnabled(true);
                    manualRadioButton.setEnabled(true);
                    timeTextField.setEnabled(true);
                    okButton.setEnabled(true);
                    startButton.setEnabled(false);
                    portTextField.setEnabled(false);
                    addressTextField.setEnabled(false);
                    stopSendButton.setEnabled(true);
                    upButton.setEnabled(true);

                    tempSensor = new TempSensor(portTextField.getText(), Long.parseLong(addressTextField.getText()));
                    tempSensor.scan();
                    tempSensor.connect();
                } catch (Exception ex) {
                    stopButton.setEnabled(false);
                    autoRadioButton.setEnabled(false);
                    manualRadioButton.setEnabled(false);
                    timeTextField.setEnabled(false);
                    tempTextField.setEnabled(false);
                    okButton.setEnabled(false);
                    startButton.setEnabled(true);
                    portTextField.setEnabled(true);
                    addressTextField.setEnabled(true);
                    stopSendButton.setEnabled(false);
                    upButton.setEnabled(false);

                    ConFailed dialog = new ConFailed();
                    dialog.setVisible(true);
                    System.out.println("Exception: " + ex);
                }

            }
        });


        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("ActionEvent: " + e);
                stopButton.setEnabled(false);
                autoRadioButton.setEnabled(false);
                manualRadioButton.setEnabled(false);
                timeTextField.setEnabled(false);
                tempTextField.setEnabled(false);
                okButton.setEnabled(false);
                startButton.setEnabled(true);
                portTextField.setEnabled(true);
                addressTextField.setEnabled(true);
                stopSendButton.setEnabled(false);
                upButton.setEnabled(false);

                tempSensor.close();

            }
        });


        autoRadioButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tempTextField.setEnabled(false);
                timeTextField.setEnabled(true);
                AutoRB = true;
            }
        });

        manualRadioButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sensorLogicThread.stopProcesing();
                tempTextField.setEnabled(true);
                timeTextField.setEnabled(false);
                AutoRB = false;

            }
        });

        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    if (AutoRB)
                        sensorLogicThread.startProcessing();
                    else {
                        Date dtn = new Date();
                        SimpleDateFormat formatter1 = new SimpleDateFormat("dd:mm:yy hh:mm:ss");
                        String date = formatter1.format(dtn);
                        temp = tempTextField.getText();
                        logTextArea.append("\n Time:" + date + "   Temperature: " + temp + "  C");

                        tempSensor.setTemp(Integer.parseInt(temp));
                        tempSensor.send(temp);
                    }
                } catch (Exception ex) {
                    ConFailed dialog = new ConFailed();
                    dialog.setVisible(true);
                    System.out.println("Exception: " + ex);
                }

            }
        });

        stopSendButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sensorLogicThread.stopProcesing();
            }
        });
    }

}


